/** */
package co.com.umobile.persistence;

import android.app.SearchManager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


/**
 * Class that managed the access to database .
 * @author sergiogranada
 * 
 */
public class DatabaseManager extends SQLiteOpenHelper {

  /** the database name. */
  private static final String DATABASE_NAME = "umobile";

  /** The columns we'll include in the dictionary table. */
  public static final String KEY_WORD = SearchManager.SUGGEST_COLUMN_TEXT_1;
  
  /** The columns we'll include in the dictionary table. */
  public static final String KEY_DEFINITION = SearchManager.SUGGEST_COLUMN_TEXT_2;

  /** the database version. */
  private static final int DATABASE_VERSION = 2;

  /** an instance of the database. */
  private static SQLiteDatabase sqliteDataBase;

  /** the database context. */
  private static Context context;

  /** used to create the singleton. */
  private static DatabaseManager instance;

  /**
   * @param context context.
   * @param name name.
   * @param factory factory.
   * @param version version.
   */
  private DatabaseManager(Context context, String name, CursorFactory factory, int version) {
    super(context, name, factory, version);
    super.getWritableDatabase();

  }

  /**
   * @return the ssf mobile database
   */
  public static SQLiteDatabase getDatabase() {
    if (sqliteDataBase == null) {
      if (instance == null) {
        instance = new DatabaseManager(context, DATABASE_NAME, null, DATABASE_VERSION);
      } else {
        sqliteDataBase = instance.getWritableDatabase();
      }
    }
    return sqliteDataBase;
  }

  /**
   * @param applicationContext applicationContext.
   * @return this controller
   */
  public static DatabaseManager getInstance(Context applicationContext) {

    DatabaseManager.context = applicationContext;

    if (instance == null) {
      instance = new DatabaseManager(DatabaseManager.context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    return instance;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
   */
  @Override
  public void onCreate(SQLiteDatabase db) {

    sqliteDataBase = db;

    db.execSQL(DatabaseScript.USER_TABLE_CREATE);
    db.execSQL(DatabaseScript.PLACE_TABLE_CREATE);
    db.execSQL(DatabaseScript.SUBJECT_TABLE_CREATE);
    db.execSQL(DatabaseScript.SUBJECT_DETAIL_TABLE_CREATE);
    db.execSQL(DatabaseScript.NOTIFICATION_TABLE_CREATE);
    
    //Default insert test
//    db.execSQL(DatabaseScript.USERTYPE_INSERT_1);
//    db.execSQL(DatabaseScript.USERTYPE_INSERT_2);
//    db.execSQL(DatabaseScript.USER_INSERT_1);
//    db.execSQL(DatabaseScript.USER_INSERT_2);
//    db.execSQL(DatabaseScript.USER_INSERT_3);
//    db.execSQL(DatabaseScript.SCHEDULE_INSERT_1);
//    db.execSQL(DatabaseScript.SCHEDULE_INSERT_2);
//    db.execSQL(DatabaseScript.SCHEDULE_INSERT_3);
//    db.execSQL(DatabaseScript.SCHEDULE_INSERT_4);
//    db.execSQL(DatabaseScript.SCHEDULE_INSERT_5);
//    db.execSQL(DatabaseScript.SCHEDULE_INSERT_6);
//    db.execSQL(DatabaseScript.PLACETYPE_INSERT_1);
//    db.execSQL(DatabaseScript.PLACETYPE_INSERT_2);
//    db.execSQL(DatabaseScript.PLACE_INSERT_1);
//    db.execSQL(DatabaseScript.PLACE_INSERT_2);
//    db.execSQL(DatabaseScript.PLACE_INSERT_3);
//    db.execSQL(DatabaseScript.PLACE_INSERT_4);
//    db.execSQL(DatabaseScript.PLACE_INSERT_5);
//    db.execSQL(DatabaseScript.PLACE_INSERT_6);
//    db.execSQL(DatabaseScript.PLACE_INSERT_7);
//    db.execSQL(DatabaseScript.SCHEDULEDETAIL_INSERT_1);
//    db.execSQL(DatabaseScript.SCHEDULEDETAIL_INSERT_2);
//    db.execSQL(DatabaseScript.SCHEDULEDETAIL_INSERT_3);
//    db.execSQL(DatabaseScript.SCHEDULEDETAIL_INSERT_4);
//    db.execSQL(DatabaseScript.SCHEDULEDETAIL_INSERT_5);
//    db.execSQL(DatabaseScript.SCHEDULEDETAIL_INSERT_6);
    
    
    instance = this;

  }

  @Override
  public void onOpen(SQLiteDatabase db) {
    try {
      sqliteDataBase.enableWriteAheadLogging();
    } catch (Exception e) {
      System.out.println();
    }
    super.onOpen(db);
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
   */
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onCreate(db);
  }

}
